use anyhow::Result;
use axum::{Router, serve, routing};

use std::sync::Arc;
use tokio::sync::Mutex;

use chapter4::routes::{
    healthcheck::healthcheck, 
    subscriptions::subscribe,
};
use chapter4::configuration::Setting;
use chapter4::database::SqliteDb;
use chapter4::telemetry::get_log_subscriber;


#[tokio::main]
async fn main() -> Result<()>{
    let _guard = get_log_subscriber()?;
    
    let configuration = Setting::default();
    let db = SqliteDb::from_setting(&configuration.database).await?;
    let db = Arc::new(Mutex::new(db));

    let app = Router::new()
        .route("/healthcheck", routing::get(healthcheck))
        .route("/subscribe", routing::post(subscribe))
        .with_state(db);
    let lisenter = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", configuration.application_port)).await?;

    serve(lisenter, app).await?;

    Ok(())
}

